<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="小程序" prop="mpId">
                            <el-select v-model="condition.search.mpId" placeholder="请选择小程序" style="width:200px;">
                                <el-option
                                    v-for="item in mpInfos"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    {{ item.name }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="appName"
                label="小程序">
            </el-table-column>
            <el-table-column
                prop="type"
                :formatter="formatType"
                label="类型">
            </el-table-column>
            <el-table-column
                prop="url"
                label="URL值">
            </el-table-column>
            <el-table-column
                prop="path"
                label="路径">
            </el-table-column>
            <el-table-column
                prop="query"
                label="参数">
            </el-table-column>
            <el-table-column
                prop="version"
                :formatter="formatVersion"
                label="版本">
            </el-table-column>
            <el-table-column
                prop="expiryType"
                :formatter="formatExpiryType"
                label="到期类型">
            </el-table-column>
            <el-table-column
                prop="expiryTime"
                label="到期时间">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="60">
                <template slot-scope="scope">
                    <div>
                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="40%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="150px" size="small">
                <el-form-item label="选择小程序">
                    <el-select v-model="addOrUpdateForm.mpId" placeholder="请选择小程序" style="width:200px;">
                        <el-option
                            v-for="item in mpInfos"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select v-model="addOrUpdateForm.type"
                               placeholder="类型">
                        <el-option label="LINK"
                                   value="LINK"></el-option>
                        <el-option label="SCHEME"
                                   value="SCHEME"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="路径">
                    <el-input v-model="addOrUpdateForm.path"
                              placeholder="路径"></el-input>
                </el-form-item>
                <el-form-item label="查询参数">
                    <el-input v-model="addOrUpdateForm.query"
                              placeholder="场景值"></el-input>
                </el-form-item>
                <el-form-item label="版本">
                    <el-select v-model="addOrUpdateForm.version"
                               placeholder="版本">
                        <el-option label="正式版"
                                   value="release"></el-option>
                        <el-option label="体验版"
                                   value="trial"></el-option>
                        <el-option label="开发版"
                                   value="develop"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="失效类型">
                    <el-select v-model="addOrUpdateForm.expiryType"
                               placeholder="失效类型">
                        <el-option label="固定时间"
                                   value="0"></el-option>
                        <el-option label="间隔天数"
                                   value="1"></el-option>
                        <el-option label="永久有效"
                                   value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="失效间隔天数" v-if="addOrUpdateForm.expiryType == 1">
                    <el-input v-model="addOrUpdateForm.expireInterval"
                              placeholder="失效间隔天数"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="过期时间" v-if="addOrUpdateForm.expiryType == 0">
                    <el-date-picker
                        v-model="addOrUpdateForm.expiryTime"
                        type="datetime"
                        placeholder="过期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addMnpLink, deleteMnpLink, getMnpLinkByPage, getMpInfoByPage, updateMnpLink} from '@/api/api'

export default {
    name: "MnpLink",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 小程序列表
            mpInfos: [],
            // 二维码图片url数组
            qrcodeUrls: [],
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        mpId: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatType: function (row, column, cellValue) {
            if (!cellValue) {
                return '';
            }
            if (cellValue == 'LINK') {
                return "LINK";
            } else if (cellValue == 'SCHEME') {
                return "SCHEME";
            } else {
                return "未知";
            }
        },
        formatExpiryType: function (row, column, cellValue) {
            if (cellValue == '0') {
                return "固定时间";
            } else if (cellValue == '1') {
                return "间隔天数";
            } else if (cellValue == '2') {
                return "永久有效";
            } else {
                return "未知";
            }
        },
        formatVersion: function (row, column, cellValue) {
            if (!cellValue) {
                return '';
            }
            if (cellValue == 'release') {
                return "正式版";
            } else if (cellValue == 'trial') {
                return "体验版";
            } else if (cellValue == 'develop') {
                return "开发版";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            this.$set(this.addOrUpdateForm, "version", 'release')
            if (this.condition.search.mpId) {
                this.$set(this.addOrUpdateForm, "mpId", this.condition.search.mpId)
            }
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addMnpLink(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateMnpLink(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteMnpLink({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取小程序列表
        getMpInfos: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {
                    accountType: 'MINI_PROGRAM'
                }
            };
            getMpInfoByPage(condition).then(data => {
                this.mpInfos = data.body.rows;
            });
        },
        // 获取表格数据
        getData: function () {
            getMnpLinkByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
                this.tableData.forEach((item, index, arr) => {
                    this.qrcodeUrls.push(item.serverUrl);
                })
            });
            this.getMpInfos();
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
